/**
 * 
 */
package com.runningfox.Process;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.runningfox.humor.AbstractProcess;
import com.runningfox.humor.Action;
import com.runningfox.humor.ProcessCallback;
import com.runningfox.logic.Cache;
import com.runningfox.logic.Logic;
import com.runningfox.model.Joke;
import com.runningfox.model.ResultSet;

/**
 * @author luhaiyang
 * load the home page
 */
public class LoadAllJokesProcess extends AbstractProcess implements ProcessCallback {

	public LoadAllJokesProcess(Handler handler) {
		super(handler);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void runImpl() {
		// TODO Auto-generated method stub
		ArrayList<Joke> list = Logic.loadJokeListFromCache();
		ResultSet res = new ResultSet();
		res.action=Action.LOADALL;
		res.jokeList=list;
		this.onSucess(res);
	}

	@Override
	public void onFailed(ResultSet res) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.LOADHOMEPAGEFAIL);
	}

	@Override
	public void onHttp200() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.HTTP200);		
	}

	@Override
	public void onHttp404() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.HTTP404);
	}

	@Override
	public void onHttp500() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.HTTP500);
	}

	@Override
	public void onSucess(ResultSet res) {
		// TODO Auto-generated method stub	
		Message msg = new Message();
		Bundle data = new Bundle();
		msg.what = Action.LOADALL;
		data.putParcelableArrayList("list", res.jokeList);
		msg.setData(data);
		handler.sendMessage(msg);
	}

	@Override
	public void onHttpFaild() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.HTTPFAIL);
	}

	@Override
	public void onParsing() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.PARSING);
	}

	@Override
	public void onParseFail() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.PARSEFAILED);
	}

}
